package cn.com.chengmandian.bike.event;

import cn.com.chengmandian.bike.utils.DingRobotUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ScanErrorEventHandler implements ApplicationListener<ScanErrorEvent> {

    @Override
    @Async
    public void onApplicationEvent(ScanErrorEvent event) {
        if(StrUtil.isBlank(event.getDdOpsToken())){
            return;
        }
        log.info("通知参数:{}",JSONObject.toJSONString(event));
        String message = "电单车离线/失联扫码提醒：\n" +
                "运营城市："+event.getAreaName()+"\n" +
                "车辆编号："+event.getBikeNo()+"\n" +
                "用户信息："+event.getMemberName()+"（"+event.getMemberPhone()+"）\n" +
                "扫码时间："+DateUtil.format(event.getDate(),"yyyy-MM-dd HH:mm:ss") +"\n" +
                "请及时查找处理。";
        try {
            DingRobotUtil.sendTextMessage(message, event.getDdOpsToken());
        }catch (Exception ex){
            log.info("通知失败:{}",ex.getMessage());
        }
    }
}
